<?php

class VersionController extends MyClass_Action {
	protected $controller;
	protected $action;
	protected $_user;
	
	public function init() {
		$this->commonAction();	
	}
	
	
	public function indexAction(){
		try {
			$count = 10;
			$curpage = ( int ) $this->_request->getParam ( 'page' );
	     	if (! $curpage)
    				$curpage = 1;
    			$offset = ($curpage - 1) * $count; 
			$sql="select  *  from version where 1 order by addtime desc limit $offset,$count";
			$result = $this->_dbAdapter->fetchAll ($sql);			
			
			$this->view->result= $result; 
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}

	public function teacherAction(){
		try {
			
			$count = 10;
			$curpage = ( int ) $this->_request->getParam ( 'page' );
	     	if (! $curpage)
    				$curpage = 1;
    			$offset = ($curpage - 1) * $count; 
			$sql="select  *  from version where version_type=2 order by addtime desc limit $offset,$count";
			$result = $this->_dbAdapter->fetchAll ($sql);			
			
			$this->view->result= $result; 
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}

	public function parentsAction(){
		try {
			$count = 10;
			$curpage = ( int ) $this->_request->getParam ( 'page' );
	     	if (! $curpage)
    				$curpage = 1;
    			$offset = ($curpage - 1) * $count; 
			$sql="select  *  from version where version_type=1 order by addtime desc limit $offset,$count";
			$result = $this->_dbAdapter->fetchAll ($sql);			
			
			$this->view->result= $result; 
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	public function addAction(){
		try {
			
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
	public function opaddAction(){
		try {
			if (! $this->isPost ()) {
					throw new Exception ( 'parameter_error' );
			}
		 	//获取post提交的值
			$p =$this->_request->getPost ();
			//	print_r($p);exit;
			
			if($p['version_no']=="" || $p['version_no']==null){
				throw new Exception ( '版本号不能为空！' );
			}
			if($p['version_url']=="" || $p['version_url']==null){
				/*
				$ext= substr(strrchr($_FILES["file"]["name"],'.'),1);//文件后缀
				if($_FILES["file"]["size"]<=0){
					throw new Exception ( '请选择文件！' );
				}
				if($ext!='apk'){
					throw new Exception ( '文件格式错误，请重新选择文件！' );
				}
				
				$allfile=substr(PROJECT_ROOT,0,-6);
				$file_size=$_FILES["file"]["size"]/1024000;//文件大小
				$file_size=substr($file_size,0,4);
				$file_name=$_FILES["file"]["name"];//文件名称				
				if($p['version_type']==1){
					$savePath=$allfile.'www/parents/';
					//$file_url='http://www.yunxiaoche.com/parents/';
				}else{
					$savePath=$allfile.'www/teacher/';
					//$file_url='http://www.yunxiaoche.com/teacher/';
				}
				move_uploaded_file($_FILES['file']['tmp_name'],$savePath.'School_Bus_P'.$p['version_no'].'.apk');
				if($p['version_type']==1){
					$file_name='http://www.yunxiaoche.com/parents/School_Bus_P'.$p['version_no'].'.apk';
				}else{
					$file_name='http://www.yunxiaoche.com/teacher/School_Bus_T'.$p['version_no'].'.apk';
				}
				$data=array(
					'version_no'=>$p['version_no'],
					'version_explain'=>$p['version_explain'],
					'version_url'=>$file_name,
					'version_type'=>$p['version_type'],
					//'file_size'=>$file_size,
					'addtime'=>time(),
				);*/
			}else{
				$file_name=$p['version_url'];
				$isfile=$this->check_remote_file_exists($file_name);
				
				if(!$isfile){
					throw new Exception ( '文件不存，请先上传文件' );
				}
				
				$data=array(
					'version_no'=>$p['version_no'],
					'version_explain'=>$p['version_explain'],
					'version_url'=>$file_name,
					'version_type'=>$p['version_type'],
					//'file_size'=>$p['version_size'],
					'addtime'=>time(),
				);
			}
				
			$result = $this->_dbAdapter->insert ('version',$data);
			if ($result) {
				$this->feedback ( 'success', 'success', '/'.$this->controller, 'tip', 3 );
			} else {
				throw new Exception ( 's_d_failure_ps3' );//操作失败,或已经删除，请先刷新查看！
			}
			
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
	//增加版本上传至云
	public function opaddyunAction(){
		try {
			if (! $this->isPost ()) {
					throw new Exception ( 'parameter_error' );
			}
		 	//获取post提交的值
			$p =$this->_request->getPost ();
			//	print_r($p);exit;
			if($p['version_no']=="" || $p['version_no']==null){
				throw new Exception ( '版本号不能为空！' );
			}
			//print_r($_FILES);exit;
			if($_FILES["file_url"]["size"]<=0){
				throw new Exception ( '请选择文件！' );
			}
			$ext= substr(strrchr($_FILES["file_url"]["name"],'.'),1);//文件后缀
			if($ext!='apk'){
				throw new Exception ( '文件格式错误，请重新选择文件！' );
			}
			//$urls=PROJECT_ROOT .'/public/apk/';
			$urls='/data/base/www/down.myapp.com/';
			if($p['version_type']==1){
				$file_name='School_Bus_P';
			}else{
				$file_name='School_Bus_T';
			}
			$yunpath=$urls.$file_name.$p['version_no'].'.apk';
			//上传文件
			move_uploaded_file($_FILES['file_url']['tmp_name'],$urls.$file_name.$p['version_no'].'.apk');
			//echo $urls.$file_name.$p['version_no'].'.apk';exit;
			if($p['version_type']==1){
				//$file_url='http://cloud.yunxiaoche.com/apk/'.$file_name.$p['version_no'].'.apk';
				$file_url='http://www.yunxiaoche.com/down.myapp.com/'.$file_name.$p['version_no'].'.apk';
			}else{
				//$file_url='http://cloud.yunxiaoche.com/apk/'.$file_name.$p['version_no'].'.apk';
				$file_url='http://www.yunxiaoche.com/down.myapp.com/'.$file_name.$p['version_no'].'.apk';
			}
			//$res=Azure_Storage::defaultImg('apk',$yunpath,$file_name.$p['version_no'].'.apk');//上传云
			//$res=Azure_Storage::defaultImg('down.myapp.com',$yunpath,$file_name.$p['version_no'].'.apk');//上传云
			//if($res){
				//删除本地图片
				//unlink($yunpath);
			//}else{
			//	throw new Exception ('上传失败：'.$res);
			//}
			$data=array(
				'version_no'=>$p['version_no'],
				'version_explain'=>$p['version_explain'],//版本说明
				'version_url'=>$file_url,//绝对地址云地址
				'version_type'=>$p['version_type'],//类别1、家长版；2、教师表
				//'file_size'=>$p['version_size'],
				'addtime'=>time(),
			);
			$result = $this->_dbAdapter->insert ('version',$data);
			if ($result) {
				//echo '1';exit;
				$this->feedback ( '上传成功地址为：'.$file_url, '上传成功', '/'.$this->controller, 'tip', 3 );
			} else {
				//echo '-1';exit;
				throw new Exception ( 's_d_failure_ps3' );//操作失败,或已经删除，请先刷新查看！
			}
			
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}

	public function deleteAction(){
		try {
			$vid = ( int ) $this->_request->getParam ( 'vid' );
			if(!$vid)
				throw new Exception ( 'parameter_error' );
			$data=array(
					//'foodinfo'=>$p['info'],
					'isdelete'=>1,
				);
			$result = $this->_dbAdapter->update ( 'version', $data,'versionid='.$vid );
			if ($result) {
				$this->feedback ( 'success', 'success', '/'.$this->controller, 'tip', 3 );
			} else {
				throw new Exception ( 's_d_failure_ps3' );//操作失败,或已经删除，请先刷新查看！
			}
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	 
	 //以下内容保持不变
	public function noRouteAction() {
		$this->_redirect ( '/' );
	}
	
	public function __call($method, $args) {
		if ('Action' == substr ( $method, - 6 )) {
			$url = '/' . $this->controller . '/index';
			return $this->_redirect ( $url );
		}
		throw new Exception ( 'Invalid method' );
	}
}

